<?php
/////////////////////////////////////////////////////
/// 函数名称：dh_static_com
/// 函数作用：产生静态的html页面的基本函数
/// 函数作者: DH
/// 作者地址: http://dhblog.org/ 
/////////////////////////////////////////////////////

//得到postid的分类链接
function get_the_category_link($postid)
{
    global $DH_archivers_url;
	$output="";
	if(get_the_category($postid))
	{
		foreach(get_the_category($postid) as $category) 
		{ 
			$output=$output.' <a href="'.$DH_archivers_url.'cat'.$category->cat_ID.'.html" title="查看分类'.$category->cat_name.'下的所有文章">'.$category->cat_name.'</a> ';
		}
	}
	return $output;
}

//得到postid的标签链接
function get_the_tag_link($post_ID)
{
    global $DH_archivers_url;
	$output="";
	if(get_the_tags($post_ID))
	{
		foreach(get_the_tags($post_ID) as $tag) 
		{ 
			$output=$output.'<a href="'.$DH_archivers_url.'tag'.$tag->term_id.'.html" title="查看标签'.$tag->name.'下的所有文章">'.$tag->name.'</a> ';
		}
	}
	return $output;
}

function archivers_tag() {
    global $wpdb, $posts, $wp_version,$DH_archivers_url;
	$DH_archivers_catalog = 'archivers';
	$category_index_all='';
    
	$sql_category = "SELECT $wpdb->terms.term_id as cat_ID, name, slug, count, description
					FROM $wpdb->terms,$wpdb->term_taxonomy
					WHERE $wpdb->term_taxonomy.term_id = $wpdb->terms.term_id and 
						  taxonomy = 'post_tag'";
	$category_index = $wpdb->get_results($sql_category);
	if($category_index) {
		foreach($category_index as $category) {
			$category_name = $category->name;
			$category_slug = $category->slug;
			$category_count = $category->count;
			$category_Id = $category->cat_ID;
			$category_description = $category->description;
			if($category_count){
                $category_index_r .= '<a href="'.$DH_archivers_url.'tag'.$category_Id.'.html" title="'.$category_description.'共有文章'.$category_count.'篇">'.$category_name.'</a>';
			}
		}
	} 
   return $category_index_r;
}

function archivers_category() 
{
    global $wpdb, $DH_archivers_url;
	$DH_archivers_catalog = 'archivers';
	$category_index_all='';
    
	$sql_category = "SELECT $wpdb->terms.term_id as cat_ID, name, slug, count, description
					FROM $wpdb->terms,$wpdb->term_taxonomy
					WHERE $wpdb->term_taxonomy.term_id = $wpdb->terms.term_id and 
						  taxonomy = 'category'";
	$category_index = $wpdb->get_results($sql_category);
	if($category_index) {
		foreach($category_index as $category) {
			$category_name = $category->name;
			$category_slug = $category->slug;
			$category_count = $category->count;
			$category_Id = $category->cat_ID;
			$category_description = $category->description;
			if($category_count){
                $category_index_r = '<li><a href="'.$DH_archivers_url.'cat'.$category_Id.'.html" title="'.$category_description.'">'.$category_name.'</a> ('.$category_count.')</li>';
                $category_index_all .=  $category_index_r."\n";
			}
		}
	} 
   //return $category_index_all;
   return wp_list_categories('title_li=&orderby=name&use_desc_for_title=false&exclude=1&echo=0');
}

function archivers_category_id($cat_ID)
{
    global $wpdb,$DH_archivers_url;
	$sql_category = "SELECT $wpdb->terms.term_id as cat_ID, name, slug, count, description
					FROM $wpdb->terms,$wpdb->term_taxonomy
					WHERE $wpdb->term_taxonomy.term_id = $wpdb->terms.term_id and 
						  taxonomy = 'category' and parent = $cat_ID";
	$category_index = $wpdb->get_results($sql_category);
	$DH_category_index_all = '';
	if($category_index) 
	{
		$DH_category_index_all .= '<ul>';
		foreach($category_index as $category) 
		{
			$count_down=archivers_category_id($category->cat_ID);
			if($category->count!=0||$count_down!='')
			{
				$category_name = $category->name;
				$category_Id = $category->cat_ID;
				$category_description = $category->description;			
				$category_index_r = '<li><a href="'.$DH_archivers_url.'cat'.$category_Id.'.html" title="'.$category_description.'">'.$category_name.'</a>';	
				$DH_category_index_all .=  $category_index_r.$count_down;
				$DH_category_index_all .= '</li>';				
			}
		}
		$DH_category_index_all .= '</ul>';
	} 
	return $DH_category_index_all;
}

function archivers_page() 
{
	global $wpdb,$DH_archivers_src_path;
	$archivers_page_all = '';
	$sql_ap = "SELECT ID, post_title
              FROM $wpdb->posts
              WHERE post_type = 'page'
              AND post_status = 'publish'
              AND post_password = ''";
    $archivers_page_index = $wpdb->get_results($sql_ap);
	if($archivers_page_index) { 
		foreach ($archivers_page_index as $page) {
            $archivers_page_url = $DH_archivers_path.'p'.$page->ID.'.html';
            $archivers_page_title = $page->post_title;
            $archivers_page_r = '<li><a href="'.$archivers_page_url.'" title="'.$archivers_page_title.'">'.$archivers_page_title.'</a></li>';
			$archivers_page_all .= $archivers_page_r."\n";
		}
	}
	 return $archivers_page_all;
}

//替换固定的字段
function dh_replace_html($html)
{
	global $DH_archivers_url;
	//替换固定字段
	//$html = str_replace("%DH_blog_home%",$DH_archivers_url,$html);
	//$html = str_replace("%DH_blog_name%",get_bloginfo('name'),$html);
	//$html = str_replace("%DH_descriptions%",get_option('blogdescription'),$html);
	return $html;
}

function DH_get_post_count($post_id) 
{
	 global $wpdb;
      $sql ="SELECT comment_count
	         From $wpdb->posts
			 where ID = '$post_id'";
	  $mycount = $wpdb->get_results($sql);
	  if($mycount){
		  foreach($mycount as $count){
			  $DH_get_post_count = $count->comment_count;
		  }
	  }
      return $DH_get_post_count;
}
function dh_file_get_contents($filename) {
	$fh = fopen($filename, 'rb', false);

	clearstatcache();
	if ($fsize = @filesize($filename)) {
		$data = fread($fh, $fsize);
	} else {
		$data = '';
		while (!feof($fh)) {
			$data .= fread($fh, 8192);
		}
	}
	fclose($fh);
	return $data;
}

function dh_file_put_contents($filename, $content) {
	
	// Open the file for writing
	$fh = @fopen($filename, 'wb', false);
	// Write to the file
	@fwrite($fh, $content);
	// Close the handle
	@fclose($fh);
}

function get_tag_name($tagid)
{
	global $wpdb;
    $sql ="SELECT name From $wpdb->terms where term_id = '$tagid'";
	$mytags = $wpdb->get_results($sql);
	if($mytags){
		foreach($mytags as $tag){
		  $DH_get_tag_name = $tag->name;
		}
	}
    return $DH_get_tag_name;
}

function pre_filter($content)
{
	return preg_replace_callback(
        "/\s*<pre(?:lang=[\"']([\w-]+)[\"']|line=[\"'](\d*)[\"']|escaped=[\"'](true|false)?[\"']|\s)+>(.*)<\/pre>\s*/siU",
        "wp_syntax_substitute", $content);
}

function get_termid_count($termid)
{
    global $wpdb;
	$SQL = "SELECT $wpdb->term_taxonomy.count 
			FROM $wpdb->term_taxonomy 
			WHERE $wpdb->term_taxonomy.term_id=$termid";
	$count=$wpdb->get_var($SQL);
	
	$SQL = "SELECT $wpdb->term_taxonomy.term_id
			FROM $wpdb->term_taxonomy
			WHERE $wpdb->term_taxonomy.parent = $termid";	
	
	$child_alls = $wpdb->get_results($SQL);
	if($child_alls) 
	{
		foreach($child_alls as $child_all) 
		{
			$count+=get_termid_count($child_all->term_id);
		}
	} 	
	return $count;
}
?>